package _18_剑指OfferII;

import java.util.HashSet;
import java.util.Set;

public class _016_剑指OfferII不含重复字符的最长子字符串 {

    public static void main(String[] args) {
        _016_剑指OfferII不含重复字符的最长子字符串 v = new _016_剑指OfferII不含重复字符的最长子字符串();

        String str = "bbbbbb";
        System.out.println(v.lengthOfLongestSubstring(str));
    }


    // 使用HashSet写法
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        char[] chars = s.toCharArray();
        int len = chars.length;
        int right = 0;
        int max = 1;
        for (int i = -1; i < len; i++) {
            if (i >= 0) {
                set.remove(chars[i]);
            }
            while (right < len && !set.contains(chars[right])) {
                set.add(chars[right]);
                right++;
            }
            max = Math.max(max, right - i);
        }
        return max;
    }

}
